---
title: File Generation Tools
description: This cookbook shows how to use the FileGenerationTool to generate various file types (JSON, CSV, PDF, TXT).
---

## Code

```python cookbook/tools/file_generation_tools.py
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.file_generation import FileGenerationTools
from agno.db.sqlite import SqliteDb

agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    db=SqliteDb(db_file="tmp/test.db"),
    tools=[FileGenerationTools(output_directory="tmp")],  
    description="You are a helpful assistant that can generate files in various formats.",
    instructions=[
        "When asked to create files, use the appropriate file generation tools.",
        "Always provide meaningful content and appropriate filenames.",
        "Explain what you've created and how it can be used.",
    ],
    markdown=True,
)

def process_file_generation_output(response, example_name: str):
    """Process and display the file generation output"""
    print(f"=== {example_name} ===")
    print(response.content)
    if response.files:
        for file in response.files:
            print(f"Generated file: {file.filename} ({file.size} bytes)")
            if file.url:
                print(f"File location: {file.url}")
    print()

if __name__ == "__main__":
    print("File Generation Tool Cookbook Examples")
    print("=" * 50)

    # JSON File Generation Example
    response = agent.run(
        "Create a JSON file containing information about 3 fictional employees with name, position, department, and salary."
    )
    process_file_generation_output(response, "JSON File Generation Example")

    # CSV File Generation Example
    response = agent.run(
        "Create a CSV file with sales data for the last 6 months. Include columns for month, product, units_sold, and revenue."
    )
    process_file_generation_output(response, "CSV File Generation Example")

    # PDF File Generation Example
    response = agent.run(
        "Create a PDF report about renewable energy trends in 2024. Include sections on solar, wind, and hydroelectric power."
    )
    process_file_generation_output(response, "PDF File Generation Example")

    # Text File Generation Example
    response = agent.run(
        "Create a text file with a list of best practices for remote work productivity."
    )
    process_file_generation_output(response, "Text File Generation Example")
```

## Usage

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Set your API key">
    ```bash
    export OPENAI_API_KEY=xxx
    ```
  </Step>

  <Step title="Install libraries">
    ```bash
    pip install -U openai agno reportlab
    ```
  </Step>

  <Step title="Run Agent">
    <CodeGroup>
    ```bash Mac
    python cookbook/tools/file_generation_tools.py
    ```

    ```bash Windows
    python cookbook/tools/file_generation_tools.py
    ```
    </CodeGroup>
  </Step>
</Steps>
